package com.etc.controller;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.etc.entity.Dept;
import com.etc.entity.Employees;
import com.etc.service.IDeptService;
import com.etc.service.IEmployeesService;
import com.etc.service.impl.DeptServiceImpl;
import com.etc.service.impl.EmployeesServiceImpl;
import com.etc.util.MD5Util;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * 邓玲琪
 * Servlet implementation class StaffsController1
 */
@WebServlet("/StaffsController")
@MultipartConfig
public class StaffsController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	//调用业务层
	IEmployeesService service=new EmployeesServiceImpl();
	IDeptService service2=new DeptServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StaffsController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op=request.getParameter("op");
		if("vaguesd".equals(op)) {
			vaguesd(request, response);			//后台员工管理分页显示

		}else if("add".equals(op)) {
			add(request, response);				//后员工管理页添加员工

		}else if("dept".equals(op)) {
			dept(request, response);			//后员工管理页添加部门选项

		}else if("modif".equals(op)) {
			modif(request, response);			//后员工管理页修改员工

		}else if("dels".equals(op)) {
			del(request, response);			//后台员工管理页删除员工(将员工状态改为离职)

		}else if("que".equals(op)) {
			que(request, response);				//后台员工管理按多条件查询
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	//后台员工管理分页显示
	private void vaguesd(HttpServletRequest request, HttpServletResponse response) throws IOException {		
		int pageNo=1; //当前页						
		if (request.getParameter("pageNo")!=null) {
			pageNo=Integer.parseInt(request.getParameter("pageNo"));
		}		
		int pageSize=Integer.parseInt(request.getParameter("pageSize"));

		PageData pageData=service.getqueryAllEmployeesd(pageNo, pageSize);
		Gson gson=new Gson();
		String json=gson.toJson(pageData);
		response.getWriter().print(json);
		response.getWriter().close();
	}

	//后员工管理页添加员工
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String deptName= request.getParameter("deptName2");
		System.out.println(deptName);
		Part part = request.getPart("empPicture");
		String path = loadPhoto(part, request, response);
		Dept dept = service2.getDeptByName(deptName);
		if (dept != null) {
			int deptId=dept.getDeptId();
			Employees employees = new Employees(request.getParameter("empName2"), request.getParameter("empPwd2"), request.getParameter("empPhone2"), null, dept, path, 1);
			employees.setEmpPwd(MD5Util.doubleEncryption(employees.getEmpPwd()));

			boolean flag=service.addEmployees(employees,deptId);   
			Gson gson=new Gson();
			String json=gson.toJson(flag);
			response.getWriter().print(json);
			response.getWriter().close();
		}
	}

	//后员工管理页添加部门选项
	private void dept(HttpServletRequest request, HttpServletResponse response) throws IOException {		
		List<Dept> dept=service2.getqueryDept();	
		Gson gson=new Gson();
		String json=gson.toJson(dept);
		response.getWriter().print(json);
		response.getWriter().close();			
	}

	//后员工管理页修改员工
	protected void modif(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String jsonString= request.getParameter("employees");		
		String deptName= request.getParameter("deptName");
		int deptId;
		List<Dept> list = service2.getqueryDept();
		for (Dept article : list) {

			if (deptName.equals(article.getDeptName())) {
				deptId=article.getDeptId();
				Gson gson=new Gson();			
				Employees employees=gson.fromJson(jsonString, Employees.class);
				employees.setEmpPwd(MD5Util.doubleEncryption(employees.getEmpPwd()));
				employees.setEmpStatus(1);
				boolean flag=service.modifyEmployees(employees, deptId); 			  
				String json=gson.toJson(flag);
				response.getWriter().print(json);
				response.getWriter().close();
			}
		}
	}

	//后台员工管理页删除员工(将员工状态改为离职)
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取用户传递过来的参数articleId
		String empId = request.getParameter("empId");
		//调用service
		boolean flag = service.delsEmployees(Integer.parseInt(empId));
		//可以根据flag的值提示用户是否删除成功
		//页面跳转
		Gson gson=new Gson();		
		String json=gson.toJson(flag);

		response.getWriter().print(json);
		response.getWriter().close();			
	}

	//后台员工管理按多条件查询
	private void que(HttpServletRequest request, HttpServletResponse response) throws IOException {		
		int pageNo=1; //当前页						
		if (request.getParameter("pageNo")!=null) {
			pageNo=Integer.parseInt(request.getParameter("pageNo"));
		}
		int pageSize=Integer.parseInt(request.getParameter("pageSize"));

		int empId1=Integer.parseInt(request.getParameter("empId1"));
		String empName1= request.getParameter("empName1");		
		int empStatus1=Integer.parseInt(request.getParameter("empStatus1"));
		String hireDate1= request.getParameter("hireDate1");
		String hireDate2= request.getParameter("hireDate2");
		String deptName1= request.getParameter("deptName1");
		int deptId1=0;
		List<Dept> list = service2.getqueryDept();	
		if (deptName1!=""){
			for (Dept article : list) {					
				if (deptName1.equals(article.getDeptName())) {
					deptId1=article.getDeptId();
					Gson gson=new Gson();					
					PageData pageData=service.queryKeyEmployeesd(pageNo, pageSize, empId1, empName1, deptId1, empStatus1, hireDate1, hireDate2);				
					String json=gson.toJson(pageData);
					response.getWriter().print(json);
					response.getWriter().close();
				}			
			}
		}
		if (deptName1==""){
			Gson gson=new Gson();				
			PageData pageData=service.queryKeyEmployeesd(pageNo, pageSize, empId1, empName1, deptId1, empStatus1, hireDate1, hireDate2);				
			String json=gson.toJson(pageData);
			response.getWriter().print(json);
			response.getWriter().close();
		}	
	}

	/**
	 * 上传文件的方法
	 * @param part 文件对象
	 * @throws IOException
	 */
	private String loadPhoto(Part part,HttpServletRequest request, HttpServletResponse response) throws IOException {
		//通过请求头和拼接获得真实的图片名
		String header = part.getHeader("content-disposition");
		String realName=header.substring(header.indexOf("filename=")+10,header.length()-1);
		//添加uuid
		UUID uuid = UUID.randomUUID();
		String uuidRealName=uuid+realName;
		System.out.println(uuidRealName);

		//拼接完整文件名
//		String photePath=request.getServletContext().getRealPath("/") + "dist\\img\\" +uuidRealName;
		String photePath="D:\\images\\" + uuidRealName;
		System.out.println(photePath);

		//写入文件
		part.write(photePath);
		//此处返回的是文件的路径片段
		return uuidRealName;
	}

}
